package scau.erps.service.impl;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.service.IdEntityService;

import scau.erps.app.Erps;
import scau.erps.entity.Incomestatement;
import scau.erps.entity.Sand;
import scau.erps.service.IncomestatementService;

/**
 * 报表——损益表实现类
 * 
 * @author FYJ
 * 
 */
public class IncomestatementServiceImpl extends IdEntityService<Incomestatement> implements IncomestatementService{

	@Override
	public List<Incomestatement> getIncomestatementOfThisYear(int teamid, String year) {
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		String thisYear = "Y"+year.charAt(1);
		if(teamid != 0){
			e1 = Cnd.exps("teamid", "=", teamid);			
		}
		if(year != null){
			e2 = Cnd.exps("year", "=", thisYear);
		}
		return this.query(Cnd.where(e1).and(e2), null);
	}

	@Override
	public void updateIncomestatement(Incomestatement incomestatement) {
		this.dao().update(incomestatement);
		
	}

	@Override
	public List<Incomestatement> getIncomestatementOfLastYear(int teamid, String year) {
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		if(teamid != 0){
			e1 = Cnd.exps("teamid", "=", teamid);			
		}
		if(year != null){
			char y = (char) (year.charAt(1)-1);
			String oldYear = "Y"+y;
			if (year.equals("Y0")) oldYear="Y8"; 
			e2 = Cnd.exps("year", "=", oldYear);
		}
		return this.query(Cnd.where(e1).and(e2), null);
	}

	@Override
	public List<Incomestatement> getIncomestatementByTeam(int teamid) {
		SqlExpressionGroup e1 = null;
		if(teamid != 0){
			e1 = Cnd.exps("teamid", "=", teamid);			
		}
		return this.query(Cnd.where(e1), null);
	}

	
	/* 支付应付税
	 * @see scau.erps.service.IncomestatementService#payTax(int)
	 */
	@Override
	public int payTax(int teamid) {
		Sand sand = Erps.getSand();
		int cash = sand.getCash();
		String year = sand.getTime();
		if (year.charAt(1) == '0')
			year = "Y" + 9;
		else
			year = "Y" + (char) (year.charAt(1));		
		int tax = getIncomestatementOfLastYear(teamid, year).get(0).getTax();
		cash = cash - tax;
		sand.setCash(cash);
		sand.setTax(tax);
		this.dao().update(sand);
		return tax;
	}
}
